package cn.ztelovecly.springboot.framework.util.pdf;

import cn.ztelovecly.springboot.framework.util.Console;

import java.io.File;
import java.io.IOException;

/**
 *
 */
public class HtmlToPdf{

    public static void go(GeneratePdfRequest request)
            throws IOException, InterruptedException {
        File file = new File("/tmp");
        if(!file.exists()) {
            file.mkdir();
        }
        Process process = Runtime.getRuntime().exec(request.getCommand());
        new Thread(new Console(process.getInputStream())).start();
        new Thread(new Console(process.getErrorStream())).start();
        process.getOutputStream().close();
        process.waitFor();
    }

    public static void go(String commandLine, String cookieMode, Long passportId, String url, String outputPath)
            throws IOException, InterruptedException {
        String exeName =
                  commandLine
               //+ " --footer-right 第[page]页/共[topage]页 "
                + " --cookie "+cookieMode+" "+passportId
                + " --no-stop-slow-scripts "
               // + " --print-media-type "
                + url + " "
                + "/tmp" + outputPath;

        Process process = Runtime.getRuntime().exec(exeName);

        new Thread(new Console(process.getInputStream())).start();
        new Thread(new Console(process.getErrorStream())).start();

        process.getOutputStream().close();
        process.waitFor();
    }

}
